package com.example.fourtestapp.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fourtestapp.R;
import com.example.fourtestapp.dbhelper.DBhelper;
import com.example.fourtestapp.entity.BillInfo;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

public class BillFragment extends Fragment {
    private View view;
    private Spinner sp;
    private Button chaxun;
    private ListView lisview;
    private List<String> paixus = new ArrayList<>();
    private Dao<BillInfo, Integer> billInfoDao;
    private List<BillInfo> billInfos = new ArrayList<>();
    private myAdpter myAdpter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    if (myAdpter == null) {
                        myAdpter = new myAdpter();
                        lisview.setAdapter(myAdpter);
                    }
                    myAdpter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bill, container, false);
        paixus.clear();
        billInfos.clear();
        billInfoDao = null;
        initView(view);
        GetData();
        initListeren();
        return view;
    }

    private void initView(View view) {
        sp = view.findViewById(R.id.sp);
        chaxun = view.findViewById(R.id.chaxun);
        lisview = view.findViewById(R.id.lisview);
        paixus.add("时间升序");
        paixus.add("时间降序");
        ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, paixus);
        sp.setAdapter(arrayAdapter);
        try {
            billInfoDao = new DBhelper(getContext()).getbilDao();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden)
            GetData();
    }

    private void GetData() {
        QueryBuilder queryBuilder = billInfoDao.queryBuilder();
        queryBuilder.selectColumns("carid", "je", "user", "time");
        try {
            billInfos = queryBuilder.query();
        } catch (Exception e) {
            e.printStackTrace();
        }
        handler.sendEmptyMessage(200);
    }

    private void GetDatabytime(Boolean bool) {
        QueryBuilder queryBuilder = billInfoDao.queryBuilder();
        queryBuilder.selectColumns("carid", "je", "user", "time").orderBy("time", bool);
        try {
            billInfos = queryBuilder.query();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initListeren() {
        chaxun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                billInfos.clear();
                String paixu = sp.getSelectedItem().toString();
                if (paixu.equals("时间降序")) {
                    GetDatabytime(true);
                } else {
                    GetDatabytime(false);
                }
                handler.sendEmptyMessage(200);
            }
        });
    }

    class myAdpter extends BaseAdapter {


        private TextView xuhao;
        private TextView carid;
        private TextView je;
        private TextView user;
        private TextView time;

        @Override
        public int getCount() {
            return billInfos.size();
        }

        @Override
        public Object getItem(int i) {
            return billInfos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.bill_list_item, viewGroup, false);
            initView(view);
            xuhao.setText((i + 1) + "");
            carid.setText(billInfos.get(i).getCarid());
            je.setText(billInfos.get(i).getJe());
            user.setText(billInfos.get(i).getUser());
            time.setText(billInfos.get(i).getTime());
            return view;
        }

        private void initView(View view) {
            xuhao = view.findViewById(R.id.xuhao);
            carid = view.findViewById(R.id.carid);
            je = view.findViewById(R.id.je);
            user = view.findViewById(R.id.user);
            time = view.findViewById(R.id.time);
        }
    }
}
